<?php
/**
 * Created by PhpStorm.
 * User: fengdh
 * Date: 2019/8/8
 * Time: 17:56
 */

namespace business\user\services;

use business\user\models\UserIdentityModel;
use yii\base\InvalidConfigException;

class UserService
{
    /**
     * userBizType  用户业务类型，1：供应2，采购
     */
    const USER_BIZ_TYPE_1 = "1";
    /**
     * userBizType  用户业务类型，1：供应2，采购
     */
    const USER_BIZ_TYPE_2 = "2";

    /**
     * userBizType  用户业务类型，1：供应2，采购
     */
    const SUPPLIER_TYPE = "1";
    /**
     * userBizType  用户业务类型，1：供应2，采购
     */
    const DEVELOPER_TYPE = "2";

    /**
     * @param $companyId
     * @return array|null|UserIdentityModel[]
     */
    public function getUserIdentityByCompanyId($companyId, $isAdmin=null)
    {
        if (empty($companyId)) {
            return null;
        }
        $query = UserIdentityModel::find();
        if ($isAdmin === null) {
            $query->where('company_id=:companyId', [':companyId'=>$companyId]);
        } else {
            $query->where('company_id=:companyId and is_admin=:isAdmin', [':companyId'=>$companyId,':isAdmin'=>$isAdmin]);
        }

        return $query->orderBy("is_admin DESC,created_time ASC")->all();
    }


    /**
     * 通过时间区间获取认证公司ID列表
     * @param string $startDate 开始时间
     * @param string $endDate
     * @return array 供应商ID列表
     * @throws InvalidConfigException
     */
    public function getUserIdentityByTimeSlot($startDate, $endDate)
    {
        $connection = \Yii::$app->get('db');
        $sql = "select company_id from `b2b_user`.`uuc_user_identity` 
                where (`created_time` >= :startDate and `created_time` <= :endDate) 
                or (`last_modified_time` >= :startDate and `last_modified_time` <= :endDate)
                group by company_id";
        return $connection->createCommand($sql)
            ->bindValue(':startDate', $startDate)
            ->bindValue(':endDate', $endDate)
            ->queryColumn();
    }
}